package com.java.ch023;

import java.util.Scanner;

class TextNumberConversion {
    public static int NumberToTen(int BeforeConversion, String number) {
        double result = 0;
        String subString;
        for (int i = 0; i < number.length(); i++) {
            subString = number.substring(i, i + 1);
            subString = sixteenCharToNumber(subString);
            result += Integer.parseInt(subString) * Math.pow(BeforeConversion, number.length() - i - 1);
        }
        return (int) result;
    }

    public static String TenToNumber(int afterConversion, String number) {
        int current = Integer.parseInt(number);
        String opResult = "";
        if (afterConversion == 16) {
            while (current >= afterConversion) {
                opResult += current % afterConversion;
                current /= afterConversion;
            }
            if (current != 0) {
                opResult += sixteenNumberToChar(current);
            }
        } else {
            while (current >= afterConversion) {
                opResult += current % afterConversion;
                current /= afterConversion;
            }
            if (current != 0) {
                opResult += current;
            }
        }
        String riResult = "";
        // 倒叙二进制字符串
        // 根据二进制的转换方式进行循环输出
        for (int i = opResult.length() - 1; i >= 0; i--) {
            riResult = riResult + opResult.substring(i, i + 1);
        }
        return riResult;
    }

    // 十六进制字母对应数字
    public static String sixteenCharToNumber(String s) {
        String num = "";
        if (s.equals("A") || s.equals("a")) {
            num = "10";
        } else if (s.equals("B") || s.equals("b")) {
            num = "11";
        } else if (s.equals("C") || s.equals("c")) {
            num = "12";
        } else if (s.equals("D") || s.equals("d")) {
            num = "13";
        } else if (s.equals("E") || s.equals("e")) {
            num = "14";
        } else if (s.equals("F") || s.equals("f")) {
            num = "15";
        } else {
            num = s;
        }
        return num;
    }

    public static String sixteenNumberToChar(int num) {
        // return "";
        String c = "";
        if (num == 10) {
            c = "A";
        } else if (num == 11) {
            c = "B";
        } else if (num == 12) {
            c = "C";
        } else if (num == 13) {
            c = "D";
        } else if (num == 14) {
            c = "E";
        } else if (num == 15) {
            c = "F";
        } else {
            c = String.valueOf(num);
        }
        return c;
    }

    public static void main(String[] args) {
        String number;
        int beforeConversion; //转换前的数制
        int afterConversion; //转换后的数制
        String stop = "";
        String result = "";
        Scanner read = new Scanner(System.in);
        do {
            System.out.println("请输入三个参数(整数):待转换的数据　转换前的数制　转换后的数制");
            number = read.next();
            beforeConversion = read.nextInt();
            afterConversion = read.nextInt();
            stop = "Q";
        } while (stop != "Q");
        try {
            if (beforeConversion != 10) {
                String temp = String.valueOf(NumberToTen(beforeConversion, number));
                result = String.valueOf(
                        TenToNumber(afterConversion, temp)
                );
            } else {
                result = String.valueOf(TenToNumber(afterConversion, number));
            }
            System.out.println(beforeConversion + "进制的数：" + number + "转换成" + afterConversion + "进制的数为：" + result);
        } catch (Exception e) {
            System.out.print("转换失败，请输入合法数据！");
            System.exit(-1);
            //TODO: handle exception
        }
    }
}